var constant = require('../../../config/constant');
var msg = require('../../../config/system_message');
var config = require('../../../config/system_config');
var Model = require('../../../components/models');
var async = require('async');

module.exports = {
    /**
     * 获取话术分类
     */
    getSpeakArtClassifications: function (req, res) {
        Model.SpeakArtClassification.findDocs(
            {$or: [{accountId: null}, {accountId: req.session.account.accountId}]},
            {},
            {sort: {type: 1, createdAt: 1}},
            function (err, docs) {
                err ? res.createFailure(err) : res.createSuccess({
                    classifications: docs.map(function (element) {
                        return {id: element._id, name: element.name};
                    }),
                    maxNumForSpeakArtClassification: config.maxNumberForSpeakArtClassification,
                    maxNumForSpeakArt: config.maxNumberForSpeakArt
                });
            });
    },

    /**
     * 添加话术分类
     */
    createSpeakArtClassification: function (req, res) {
        var name = req.body['name'];
        if (!name) return res.createFailure({code: msg.CODE.PARAM_ERROR, message: msg.ERR_MSG.PARAMS});
        Model.SpeakArtClassification.createDoc({
            name: name,
            type: constant.SpeakArtClassificationType.CUSTOM,
            accountId: req.session.account.accountId
        }, function (err, doc) {
            err ? res.createFailure(err) : res.createSuccess({classification: {id: doc._id, name: doc.name}});
        });
    },

    /**
     * 编辑话术分类
     */
    updateSpeakArtClassification: function (req, res) {
        var id = req.body['id'], name = req.body['name'];
        if (!id || !name) return res.createFailure({code: msg.CODE.PARAM_ERROR, message: msg.ERR_MSG.PARAMS});
        Model.SpeakArtClassification.updateDocs({_id: id}, {name: name}, function (err) {
            res.createResult(err);
        });
    },

    /**
     * 删除话术分类
     */
    deleteSpeakArtClassification: function (req, res) {
        var id = req.body['id'];
        if (!id) return res.createFailure({code: msg.CODE.PARAM_ERROR, message: msg.ERR_MSG.PARAMS});
        async.parallel([
            function (callback) {
                // 删除话术
                Model.SpeakArt.removeDocs({classificationId: id}, callback);
            },
            function (callback) {
                // 删除话术分类
                Model.SpeakArtClassification.removeDocs({_id: id}, callback);
            }
        ], function (err) {
            res.createResult(err);
        });
    },

    /**
     * 获取话术
     */
    getSpeakArts: function (req, res) {
        var classId = req.query['classId'];
        if (!classId) return res.createFailure({code: msg.CODE.PARAM_ERROR, message: msg.ERR_MSG.PARAMS});
        Model.SpeakArt.findDocs({classificationId: classId}, {}, {sort: {createdAt: 1}}, function (err, docs) {
            err ? res.createFailure(err) : res.createSuccess({
                speakArts: docs.map(function (element) {
                    return {id: element._id, content: element.content};
                })
            });
        });
    },

    /**
     * 添加话述
     */
    createSpeakArt: function (req, res) {
        var classId = req.body['classId'], content = req.body['content'];
        if (!classId || !content) return res.createFailure({code: msg.CODE.PARAM_ERROR, message: msg.ERR_MSG.PARAMS});
        Model.SpeakArt.createDoc({classificationId: classId, content: content}, function (err, doc) {
            err ? res.createFailure(err) : res.createSuccess({speakArt: {id: doc._id, content: doc.content}});
        });
    },

    /**
     * 编辑话术
     */
    updateSpeakArt: function (req, res) {
        var id = req.body['id'], content = req.body['content'];
        if (!id || !content) return res.createFailure({code: msg.CODE.PARAM_ERROR, message: msg.ERR_MSG.PARAMS});
        Model.SpeakArt.updateDocs({_id: id}, {content: content}, function (err) {
            res.createResult(err);
        });
    },

    /**
     * 删除话术
     */
    deleteSpeakArt: function (req, res) {
        var id = req.body['id'];
        if (!id) return res.createFailure({code: msg.CODE.PARAM_ERROR, message: msg.ERR_MSG.PARAMS});
        Model.SpeakArt.removeDocs({_id: id}, function (err) {
            res.createResult(err);
        });
    }
};
